﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using UserControl = System.Windows.Controls.UserControl;

namespace NewChillers.Views
{
    /// <summary>
    /// HelpPage.xaml 的交互逻辑
    /// </summary>
    public partial class HelpDocPage : UserControl
    {

        private bool _isLoaded = false;
        private ScrollViewer _scrollViewer;

        public HelpDocPage()
        {
            InitializeComponent();
            this.Loaded += HelpDocPage_Loaded;
        }

        private static HelpDocPage instance;
        public static HelpDocPage Instance
        {
            get
            {
                if (instance == null)
                    instance = new HelpDocPage();
                return instance;
            }
        }

        private void HelpDocPage_Loaded(object sender, RoutedEventArgs e)
        {
            
            //string filePath = Environment.CurrentDirectory + "C:\\资料\\公司文件\\充排气\\斯特林充排气项目\\技术要求-斯特林制冷机充排气台.pdf";
            string filePath = "C:\\资料\\公司文件\\充排气\\斯特林充排气项目\\技术要求-斯特林制冷机充排气台.pdf";
            try
            {
                moonPdfPanel.OpenFile(filePath);
                _isLoaded = true;
                // 调整 PDF 缩放比例以铺满整个屏幕
                moonPdfPanel.ZoomToHeight();
                moonPdfPanel.ZoomToWidth();
                UpdatePageInfo();

                // 查找 ScrollViewer 并订阅 ScrollChanged 事件
                _scrollViewer = FindScrollViewer(moonPdfPanel);
                if (_scrollViewer != null)
                {
                    _scrollViewer.ScrollChanged += ScrollViewer_ScrollChanged;
                }
            }
            catch (Exception ex)
            {
                // 处理异常，例如显示错误消息
                System.Windows.MessageBox.Show($"无法加载PDF文件: {ex.Message}");
                _isLoaded = false;
            }
        }

        private ScrollViewer FindScrollViewer(DependencyObject parent)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                if (child is ScrollViewer)
                {
                    return (ScrollViewer)child;
                }

                var result = FindScrollViewer(child);
                if (result != null)
                {
                    return result;
                }
            }
            return null;
        }

        private void UpdatePageInfo()
        {
            if (moonPdfPanel != null && moonPdfPanel.CurrentSource != null)
            {
                int totalPages = moonPdfPanel.TotalPages;
                int currentPage = moonPdfPanel.GetCurrentPageNumber();
                currentPageTextBlock.Text = currentPage.ToString();
                totalPagesTextBlock.Text = totalPages.ToString();
            }
        }

        private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            UpdatePageInfo();
        }

        private void GoButton_Click(object sender, RoutedEventArgs e)
        {
            if (int.TryParse(jumpPageTextBox.Text, out int page))
            {
                if (page > 0 && page <= moonPdfPanel.TotalPages)
                {
                    moonPdfPanel.GotoPage(page);
                    UpdatePageInfo();
                    jumpPageTextBox.Text = "";
                }
                else
                {
                    System.Windows.MessageBox.Show("请输入有效的页码。");
                }
            }
            else
            {
                System.Windows.MessageBox.Show("请输入有效的页码。");
            }
        }

        private void GotoStartPage_Click(object sender, RoutedEventArgs e)
        {
            moonPdfPanel.GotoFirstPage();
            UpdatePageInfo();
        }

        private void GotoEndPage_Click(object sender, RoutedEventArgs e)
        {
            moonPdfPanel.GotoLastPage();
            UpdatePageInfo();
        }

        private void GotoPreviousPage_Click(object sender, RoutedEventArgs e)
        {
            moonPdfPanel.GotoPreviousPage();
            UpdatePageInfo();
        }

        private void GotoNextPage_Click(object sender, RoutedEventArgs e)
        {
            moonPdfPanel.GotoNextPage();
            UpdatePageInfo();
        }

        //private void ChangePdf_Click(object sender, RoutedEventArgs e)
        //{
        //    OpenFileDialog dialog = new OpenFileDialog();
        //    dialog.InitialDirectory = Environment.CurrentDirectory + "\\帮助文档";
        //    if (dialog.ShowDialog() == DialogResult.OK)
        //    {
        //        string filePath = dialog.FileName;
        //        try
        //        {
        //            moonPdfPanel.OpenFile(filePath);
        //            _isLoaded = true;
        //        }
        //        catch (Exception)
        //        {
        //            _isLoaded = false;
        //        }
        //    }
        //}
    }
}
